package com.haven123.cloudapp.gateway.filter;

import com.alibaba.fastjson.JSON;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Log4j2
@Component
public class AccessFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run()  {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();

        log.info("send {} request to {}", request.getMethod(), request.getRequestURL().toString());
        log.info("session:{}  , sesssionvalue:{}", JSON.toJSONString(request.getRequestedSessionId()), JSON.toJSONString(request.getSession()));
        log.info("cookie:{}",JSON.toJSONString(request.getCookies()));
        if (request.getRequestURI().endsWith("/v2/api-docs")) {
            log.info("access token not necessary");
            return null;
        }

        /*
        Object accessToken = request.getParameter("accessToken");
        if (accessToken == null) {
            log.warn("access token is empty");
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(401);
            return null;
        }
        */
        log.info("access token ok");
        return null;
    }
}
